package com.consul.task;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DataflowJobTask implements DataflowJob<Object> {
    static int i=1;
    @Override
    public List<Object> fetchData(ShardingContext shardingContext) {
        System.out.println(String.format("------获取数据流-----Thread ID: %s, 任务总片數: %s, " +
                        "当前分片項: %s.当前參數: %s," +
                        "当前任務名稱: %s,当前任務參數: %s, i值: %s",
                Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter(),
                i
        ));
        List<Object> list = new ArrayList<>();
        if(i!=10) {
            list.add("流数据1");
            list.add("流数据2");
            list.add("流数据3");
            list.add("流数据4");
        }else {
            list=new ArrayList<>();
            i=1;
        }
        i++;
        return list;
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Object> list) {
        System.out.println(String.format("------处理数据流-----Thread ID: %s, 任务总片數: %s, " +
                        "当前分片項: %s.当前參數: %s," +
                        "当前任務名稱: %s.当前任務參數: %s" + " 数据为: %s",
                Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter(),
                JSON.toJSONString(list)
        ));
        System.out.println();
    }
}
